package com.itheima.wemedia.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageReqDto;
import com.itheima.wemedia.mapper.WmNewsMaterialMapper;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.pojo.WmNewsMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Value("${fastdfs-server}")
    private String fastdfsServer;

    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;

    @Override
    public PageResultVo pagelist(WmMaterialPageReqDto dto) {
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
       wrapper.eq(0 != dto.getIsCollection(),WmMaterial::getIsCollection,dto.getIsCollection());

       page(page,wrapper);

        List<WmMaterial> records = page.getRecords();
        if (!CollectionUtils.isEmpty(records)){
            records.forEach(m->m.setUrl(fastdfsServer+m.getUrl()));
        }
        return PageResultVo.pageResult(dto.getPage(),dto.getSize(),page.getTotal(),page.getRecords());


    }

    @Override
    public void collenctById(Integer id) {
        WmMaterial material = getById(id);
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        wrapper.eq(WmMaterial::getId,id);

        material.setIsCollection(1);

        update(material,wrapper);
    }

    @Override
    public void cancelById(Integer id) {
        WmMaterial material = getById(id);
        LambdaQueryWrapper<WmMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmMaterial::getUserId,RequestContextUtil.getUserId());
        wrapper.eq(WmMaterial::getId,id);

        material.setIsCollection(0);

        update(material,wrapper);

    }

    @Override
    public void delById(Integer id) {
        WmMaterial material = getById(id);
        LambdaQueryWrapper<WmNewsMaterial> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNewsMaterial::getMaterialId,id);
        WmNewsMaterial wmNewsMaterial = wmNewsMaterialMapper.selectOne(wrapper);
        if (wmNewsMaterial!=null){
            throw new LeadNewsException("已存在文章不可以删除");
        }
        LambdaQueryWrapper<WmMaterial> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(WmMaterial::getUserId,RequestContextUtil.getUserId());
        wrapper1.eq(WmMaterial::getId,id);
        remove(wrapper1);
    }


}
